/**
 * @param {number} n
 * @param {number[]} ranges
 * @return {number}
 */
var minTaps = function (n, ranges) {
  const rightMost = new Array(n + 1).fill(0).map((_, i) => i);
  for (let i = 0; i <= n; i++) {
    const start = Math.max(0, i - ranges[i]);
    const end = Math.min(n, i + ranges[i]);
    rightMost[start] = Math.max(rightMost[start], end);
  }
  let last = 0,
    ret = 0,
    pre = 0;
  for (let i = 0; i < n; i++) {
    last = Math.max(last, rightMost[i]);
    if (i === last) {
      return -1;
    }
    if (i === pre) {
      ret++;
      pre = last;
    }
  }
  return ret;
};

console.log(minTaps(7, [1, 2, 1, 0, 2, 1, 0, 1]));
